package com.hjjt.baselib.util

import android.graphics.*
import android.graphics.drawable.Drawable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.util.Log
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.gif.GifDrawable


/**
 *
 * @ProjectName:    IntelligentTerminal
 * @Package:        com.hjjt.baselib.util
 * @ClassName:      BindAdapter
 * @Description:    databinding 扩展
 * @Author:         孙浩
 * @CreateDate:     2022/6/15 16:53
 */
@BindingAdapter(
    value = ["imageUrl", "placeholder", "radius", "isRound", "gray"],
    requireAll = false
)
fun loadImage(
    imageView: ImageView,
    url: String?,
    placeholder: Drawable?,
    radius: Int,
    isRound: Boolean,
    isGray: Boolean,
) {
    if (isRound) {
        imageView.loadRoundImg(url ?: "", placeholder)
    } else {
        imageView.loadImage(url ?: "", placeholder, radius, isGray)
    }
}

@BindingAdapter(value = ["imgId", "gray"], requireAll = false)
fun loadImageWithId(imageView: ImageView, imgId: Int, isGray: Boolean) {
    imageView.loadImageWithId(imgId, isGray)
}

@BindingAdapter("objectToString")
fun objectToString(textView: TextView, objectToString: Any) {
    textView.text = objectToString.toString()
}

@BindingAdapter("percent")
fun percentToString(textView: TextView, percent: Int) {
    textView.text = "${percent}%"
}

@BindingAdapter("bitmapId")
fun setImageById(imageView: ImageView, id: Int) {
    imageView.setImageResource(id)
}

@BindingAdapter("viewSelected")
fun viewSelected(v: View, check: Boolean) {
    v.isSelected = check
}

@BindingAdapter(value = ["gifId", "autoPlay"], requireAll = false)
fun loadGif(v: ImageView, gifId: Int, autoPlay: Boolean?) {
    Glide.with(v).asGif().load(gifId).into(v)
    var play = autoPlay ?: false
    gifPlay(v, play)
}

@BindingAdapter("gifPlay")
fun gifPlay(v: ImageView, gifPlay: Boolean) {
    runCatching {
        if (v.drawable is GifDrawable) {
            val drawable = v.drawable as GifDrawable
            if (gifPlay) {
                drawable.start()
            } else {
                drawable.stop()
            }
        }
    }
}


@BindingAdapter(value = ["startColor", "endColor", "colorType"], requireAll = false)
fun gradientColor(textView: TextView, startColor: Int, endColor: Int, type: Int?) {
    val width = textView.paint.measureText(textView.text.toString()) //测量文本 宽度
    val horizontal = type ?: 0
    val y1 = textView.paint.textSize //测量文本 高度
    var startX = 0f;
    var starty = 0f;
    var endX = 0f;
    var endY = 0f;
    when (horizontal) {
        0 -> { //纵向
            startX = 0f
            starty = 0f
            endX = 0f
            endY = y1
        }

        1 -> { //横向
            startX = 0f
            starty = 0f
            endX = width
            endY = 0f
        }

        2 -> { //左上到右下
            startX = 0f
            starty = 0f
            endX = width
            endY = y1
        }
    }
    val topToBottomLG = LinearGradient(
        startX,
        starty,
        endX,
        endY,
        Color.parseColor("#FFFF0000"),
        Color.parseColor("#FFFF0000"),
        Shader.TileMode.CLAMP
    )
    textView.paint.apply {
        shader = topToBottomLG
        bgColor = Color.parseColor("#FF000000")
    }
    textView.invalidate()
}

/**
 * 富文本转换
 * */
@BindingAdapter(value = ["richText"], requireAll = false)
fun loadRichTextByWeb(wv: WebView, richText: String?) {
    if (richText == null) return
    wv.setWebViewClient(object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    })
    wv.webChromeClient = WebChromeClient()
    wv.setBackgroundColor(0)
    wv.background.alpha = 0
    var mRichText =
        "<p>\n" +
                "    <span style=\"color: #FF0000;\"><strong>123123123</strong></span>\n" +
                "</p>\n" +
                "<p>\n" +
                "    <span style=\"color: #FF0000;\"><strong>测试一下看看这里的数据对不对</strong></span>\n" +
                "</p>";

    wv.settings.run {
        javaScriptEnabled = true;//允许使用js
//不支持屏幕缩放
        setSupportZoom(false);
        builtInZoomControls = false;
//不显示webview缩放按钮
        displayZoomControls = false;
    }
    wv.loadDataWithBaseURL(null, richText, "text/html", "utf-8", null);//加载html数据
}

@BindingAdapter("spbText")
fun setTextSpannableString(textView: TextView, spannableString: SpannableStringBuilder) {
    textView.text = spannableString
}
